import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Cookie from 'js-cookie'

export const FooterList = [
      {
        path: '/main',
        name: 'mian',
        component: () => import('../views/Home/children/MainView.vue'),
        meta: {
          label: '首页'
        }
      },
      {
        path: '/shopCar',
        name: 'shopCar',
        component: () => import('../views/Home/children/ShopCar.vue'),
        meta: {
          label: '购物车'
        }
      },
      {
        path: '/my',
        name: 'my',
        component: () => import('../views/Home/children/MyView.vue'),
        meta: {
          label: '我的'
        }
      }
    ]

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: () => import('../views/Home/HomeView.vue'),
    children: FooterList
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login/LoginView.vue')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


// 路由拦截
router.beforeEach((to, from) => {
  if(!Cookie.get('token') && to.path !== '/login') {
    return '/login'
  }

  return true
})

export default router
